package com.xiaohudie.JUC;

import lombok.SneakyThrows;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author xiao hu die
 * @Date 2022/1/12 22:46
 * @Version 1.0
 */
class Share4{
    private Integer a  =1 ;
    private Lock lock =new ReentrantLock();
    private Condition condition1 =lock.newCondition();
    private Condition condition2 =lock.newCondition();
    private Condition condition3 =lock.newCondition();
 @SneakyThrows
 public void A() {
     lock.lock();
     try {
         while (a != 1) {
             condition1.await();
         }
         for (int i = 0; i < 5; i++) {
             System.out.println(Thread.currentThread().getName());
         }
         a = 2;
         condition2.signal();
     } finally {
         lock.unlock();
     }
 }
     @SneakyThrows
     public void B() {
         lock.lock();
         try {
             while (a != 2) {
                condition2.await();
             }
             for (int i = 0; i < 5; i++) {
                 System.out.println(Thread.currentThread().getName());
             }
             a = 3;
             condition3.signal();
         } finally {
             lock.unlock();
         }
     }
     @SneakyThrows
         public void C(){
             lock.lock();
             try {
                 while (a!=3){
                   condition3.await();
                 }
                 for (int i = 0; i < 5; i++) {
                     System.out.println(Thread.currentThread().getName());
                 }
                 a=1;
                 condition1.signal();
             } finally {
                 lock.unlock();
             }

 }
}
public class ThreadDemo04 {
    public static void main(String[] args) {
        Share4 share4 = new Share4();
        new Thread(()->{
            share4.A();
        },"AA").start();
        new Thread(()->{
            share4.B();
        },"BB").start();
        new Thread(()->{
            share4.C();
        },"CC").start();
    }
}
